﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Windows.Networking.Proximity;

namespace InTheHand.Phone.Tasks
{
    /// <summary>
    /// Allows an application to launch a dialog that enables the user to share a link using Tap+Send (NFC).
    /// </summary>
    public sealed class ShareNfcLinkTask
    {
        private ProximityDevice device;

        /// <summary>
        /// Initializes a new instance of the <see cref="ShareNfcLinkTask"/> class.
        /// </summary>
        public ShareNfcLinkTask()
        {
            try
            {
                device = ProximityDevice.GetDefault();
            }
            catch
            {
                throw new InvalidOperationException("NFC not supported");
            }

            if (device == null)
            {
                throw new InvalidOperationException("NFC not supported");
            }
        }

        /// <summary>
        /// Gets or sets the link URI that will be displayed in the link sharing dialog.
        /// </summary>
        public System.Uri LinkUri { set; get; }

        /// <summary>
        /// Causes the sharing dialog to be displayed to the user.
        /// </summary>
        public void Show()
        {
            if (LinkUri == null)
            {
                throw new ArgumentNullException("LinkUri");
            }

            PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual;
            frame.Navigate(new Uri(string.Format("/InTheHand.Phone.Nfc;component/Phone/Tasks/ShareNfcLinkTaskPage.xaml?uri={0}", LinkUri), UriKind.Relative));
        }
    }
}
